<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:54
 */

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;

use App\Http\Services\RoomMsgServices;
use App\Http\Services\RoomServicesInit;
use App\Http\Services\RoomWaitbitServices;
use Illuminate\Http\Request;
use App\Lib\Response;


class RoomWaitbitController extends Controller
{
    private $services_room_waitbit;
    private $services_room_msg;
    public function __construct()
    {
        $this->services_room_waitbit = RoomServicesInit::createWaitbit();
        $this->services_room_msg = new RoomMsgServices();
    }

    /**
     * 待麦列表
     * @param Request $request
     */
    public function get_users(Request $request){
        $room_id = $request->get("room_id");
        //$guard_type = $request->get("guard_type");
        //$guardTypeId =  $request->get("guard_type_id") ?? -1;
        if ($room_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        /*
        if ($guard_type == null){
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        */
        $room_id = intval($room_id);
        //$guard_type = intval($guard_type);
        $data = $this->services_room_waitbit->get_users($room_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        Response::returnResult(Hint::SUCCESS, $data);
    }


    /**
     * 请求上麦
     * @param Request $request
     */
    public function apply(Request $request){
        $room_id = $request->get("room_id");
        $guard_type = $request->get("guard_type");
        if ($room_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        if ($guard_type === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        $room_id = intval($room_id);
        $guard_type = intval($guard_type);
        $user_id = null;
        $data = $this->services_room_waitbit->apply($room_id, $guard_type, $user_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($data === Hint::ROOM_WAITBIT_IS_EXIST){
            Response::returnResult(Hint::ROOM_WAITBIT_IS_EXIST);
            return;
        }
        if ($data === Hint::ROOM_MICRO_USER_IS_EXIST){
            Response::returnResult(Hint::ROOM_MICRO_USER_IS_EXIST);
            return;
        }
        if ($data === Hint::ROOM_MICRO_GUARD_MATCH){
            Response::returnResult(Hint::ROOM_MICRO_GUARD_MATCH);
            return;
        }
        if ($data === Hint::ROOM_HOSTMICRO_IS_NULL){
            Response::returnResult(Hint::ROOM_HOSTMICRO_IS_NULL);
            return;
        }
        $this->services_room_msg->broadcast_micro_waitbit_add($room_id, $user_id, $guard_type);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 允许上麦
     * @param Request $request
     */
    public function accept(Request $request){
        $room_id = $request->get("room_id");
        $user_id = $request->get("user_id");
        if ($room_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        if ($user_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        $room_id = intval($room_id);
        $user_id = intval($user_id);
        $micro_id = null;
        $data = $this->services_room_waitbit->accept($room_id, $user_id, $micro_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        if ($data === Hint::ROOM_WAITBIT_NOT_EXIST){
            Response::returnResult(Hint::ROOM_WAITBIT_NOT_EXIST);
            return;
        }
        if ($data === Hint::ROOM_MICRO_IS_FULL){
            Response::returnResult(Hint::ROOM_MICRO_IS_FULL);
            return;
        }
        if ($data === Hint::NOT_ALLOWED){
            Response::returnResult(Hint::NOT_ALLOWED);
            return;
        }
        $this->services_room_msg->command_micro_into($room_id, $micro_id, $user_id);
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 拒绝上麦
     * @param Request $request
     */
    public function reject(Request $request){
        $room_id = $request->get("room_id");
        $user_id = $request->get("user_id");
        if ($room_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        if ($user_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        $room_id = intval($room_id);
        $user_id = intval($user_id);
        $data = $this->services_room_waitbit->reject($room_id, $user_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        $this->services_room_msg->broadcast_micro_waitbit_del($room_id, $user_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 取消上麦
     * @param Request $request
     */
    public function cancel(Request $request){
        $room_id = $request->get("room_id");
        if ($room_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        $room_id = intval($room_id);
        $user_id = null;
        $data = $this->services_room_waitbit->cancel($room_id, $user_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        $this->services_room_msg->broadcast_micro_waitbit_del($room_id, $user_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 清理用户
     * @param Request $request
     */
    public function clean(Request $request){
        $room_id = $request->get("room_id");
        if ($room_id === null){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        $room_id = intval($room_id);
        $data = $this->services_room_waitbit->clean($room_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        $this->services_room_msg->broadcast_micro_waitbit_clean($room_id);
        Response::returnResult(Hint::SUCCESS);
    }


}